package com.pd.modules.server;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

public class ConnectedClients extends Observable
{
	// Private constructor prevents instantiation from other classes
	private ConnectedClients()
	{

	}

	/**
	 * SingletonHolder is loaded on the first execution of
	 * Singleton.getInstance() or the first access to SingletonHolder.INSTANCE,
	 * not before.
	 */
	private static class SingletonHolder
	{
		public static final ConnectedClients instance = new ConnectedClients();
	}

	public static ConnectedClients getInstance()
	{
		return SingletonHolder.instance;
	}

	private Map<Long, ConnectedClientInfo> mClients = new HashMap<Long, ConnectedClientInfo>();

	public synchronized void addClient(String _username, String _ip, long _threadID)
	{
		mClients.put(_threadID, new ConnectedClientInfo(_username, _ip, _threadID));

		setChanged();
		notifyObservers();
	}

	public synchronized void removeClient(long _threadID)
	{
		mClients.remove(_threadID);
		
		setChanged();
		notifyObservers();
	}

	public synchronized ConnectedClientInfo[] getAllClients()
	{
		ConnectedClientInfo[] tmpArray = new ConnectedClientInfo[mClients.size()];
		mClients.values().toArray(tmpArray);

		return tmpArray;
	}
}
